var rootPath = getRootPath();

$(function() {
	$("#oaTable").showLoading();
	$.ajax({
		url: rootPath + '/hrmResource/queryPageBoundHrmResources',
        type: 'POST',
        contentType : 'application/json',
        data : JSON.stringify({
        	"pageNo" : 1
        }),
        success: function (data) {
        	$("#oaTable").hideLoading();
        	fillHrmResource(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	$("#oaTable").hideLoading();
        	layer.alert("error:(");
        }
    });
	
	$("#searchHrmResource").click(function() {
		$("#oaTable").showLoading();
		var loginid = $("#loginid").val() != "" ? $("#loginid").val() : null;
		$.ajax({
			url: rootPath + '/hrmResource/queryPageBoundHrmResources',
	        type: 'POST',
	        contentType : 'application/json',
	        data : JSON.stringify({
				"loginid" : loginid
			}),
	        success: function (data) {
	        	$("#oaTable").hideLoading();
	        	fillHrmResource(data);
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	$("#oaTable").hideLoading();
	        	layer.alert("error:(");
	        }
	    });
	});
	
	$("#loginid").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchHrmResource").click();
		}
	});
});

function fillHrmResource(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr = "";
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' id='hrmResource_" + data.returnObject.list[i].id +"' name='hrmResourceTr'>";
			tr += "<td>" + data.returnObject.list[i].id + "</td>";
			tr += "<td>" + data.returnObject.list[i].loginid + "</td>";
		    tr += "<td>" + data.returnObject.list[i].textfield3 + "</td><td class='f-14'>";
			tr += "<a title='解绑OA账号' href='javascript:;' onclick='hrmResource_unbind(this," + data.returnObject.list[i].id + ")' style='text-decoration:none'><i class='iconfont marginLeft12' style='color:#333'>&#xe618;</i></a></td></tr>";
		}
		$("#hrmResources").empty();
		$("#hrmResources").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").mouseover(function() {
			$(this).css("cursor", "pointer");
		});
	} else {
		layer.alert(data.returnMessage);
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var loginid = $("#loginid").val();
	$.ajax({
		url : rootPath + '/hrmResource/queryPageBoundHrmResources',
		type : 'POST',
		contentType : 'application/json',
		data : JSON.stringify({
			"loginid" : loginid,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillHrmResource(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function hrmResource_unbind(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('解绑OA账号须谨慎，确认要解绑吗？',function(index){
		$.ajax({
			url: rootPath + '/hrmResource/unBindOaAccount?id=' + id,
			type: 'GET',
			dataType: 'json',
			success: function(data){
				if(data.returnCode == "0000") {						
					$(obj).parents("tr").remove();
					layer.msg('已解绑!',{icon:1,time:1000});
					$("#dataNum").html(dataNum - 1);
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error:function(data) {
				layer.aler("error:(");
			},
		});
	});
}